﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace HDJ.Framework.Core
{
    public class LogWindow : MonoBehaviourExtend
    {
        public LoopListView2 mLoopListView;
        public Text Text_ShowInfo_Error;
        public Text Text_ShowInfo_Warning;
        public Text Text_ShowInfo_Log;
        public GameObject ShowDetails;
        public Text Text_ShowDetailsContent;

        public List<LogType> logTypes = new List<LogType>() {  LogType.Assert, LogType.Exception, LogType.Error, LogType.Log, LogType.Warning};

        private List<LogInfo> logInfos = new List<LogInfo>();
        private void Start()
        {
           
        }
       private bool isInit = false;
        // Use this for initialization
        public void Open()
        {
            gameObject.SetActive(true);
            ShowDetails.SetActive(false);

            
            LogManager.GetLoginfos(logTypes, logInfos);
            if (!isInit)
            {
                mLoopListView.InitListView(logInfos.Count, onGetItemByIndex);
                isInit = true;
            }
            LogManager.OnAddLogInfo += OnAddLogInfo;

            

            UpdateLogNumberText();
        }

        private void UpdateLogNumberText()
        {
            Text_ShowInfo_Error.text = GetNumberText(LogManager.GetLogInfoCount(LogType.Error));
            Text_ShowInfo_Warning.text = GetNumberText(LogManager.GetLogInfoCount(LogType.Warning));
            Text_ShowInfo_Log.text = GetNumberText(LogManager.GetLogInfoCount(LogType.Log));
        }
        private string GetNumberText(int num)
        {
            if (num < 999)
                return num.ToString();
            else
                return "999+";
        }
        private void OnAddLogInfo(LogInfo t)
        {
            if (logTypes.Contains(t.logType))
            {
                logInfos.Add(t);
            }
             mLoopListView.SetListItemCount(logInfos.Count+1, false);
            UpdateLogNumberText();
        }

        private LoopListViewItem2 onGetItemByIndex(LoopListView2 listView, int index)
        {
            if (index < 0 || index >= logInfos.Count)
            {
                return null;
            }

            LoopListViewItem2 item = listView.NewListViewItem("LogItems");
            LogItems itemScript = item.GetComponent<LogItems>();
            if (item.IsInitHandlerCalled == false)
            {
                item.IsInitHandlerCalled = true;
                itemScript.Init();
            }

            itemScript.Show(this, logInfos[index]);
            return item;
        }
        #region 显示log界面操作
        public void OnToggle_ShowInfoError_Change(bool isOn)
        {
            if (!logTypes.Contains(LogType.Error))
            {
                logTypes.Add(LogType.Assert);
                logTypes.Add(LogType.Error);
                logTypes.Add(LogType.Exception);
            }
            else
            {
                logTypes.Remove(LogType.Assert);
                logTypes.Remove(LogType.Error);
                logTypes.Remove(LogType.Exception);
            }

            ResetLogInfoList();
        }
        public void OnToggle_ShowInfoWarning_Change(bool isOn)
        {
            if (!logTypes.Contains(LogType.Warning ))
            {
                logTypes.Add(LogType.Warning);
            }
            else
            {
                logTypes.Remove(LogType.Warning);
            }

            ResetLogInfoList();
        }
        public void OnToggle_ShowInfoLog_Change(bool isOn)
        {
            if (!logTypes.Contains( LogType.Log))
            {
                logTypes.Add(LogType.Log);
            }
            else
            {
                logTypes.Remove(LogType.Log);
            }
            ResetLogInfoList();
        }

        private void ResetLogInfoList()
        {
            LogManager.GetLoginfos(logTypes, logInfos);
            mLoopListView.SetListItemCount(logInfos.Count,true);
        }

        public void OnButtonClear()
        {
            LogManager.Clear();
            logInfos.Clear();
            mLoopListView.SetListItemCount(0);
            UpdateLogNumberText();
        }
        #endregion

        #region 显示详细信息面板
        public void ShowDetailLog(LogInfo logInfo)
        {
            ShowDetails.SetActive(true);
            Text_ShowDetailsContent.text = logInfo.ToString();
        }
        public void OnButtonCloseShowDetails()
        {
            ShowDetails.SetActive(false);
        }
        #endregion
        public void Close()
        {
            LogManager.OnAddLogInfo -= OnAddLogInfo;
            gameObject.SetActive(false);
        }
    }
}